﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Text;

namespace DAO
{
    public class UserDAO
    {
        WebChatDBEntities db = new WebChatDBEntities();

        public User LogUserIn(string userName, string password)
        {
            User user = db.Users.SingleOrDefault(u => u.UserName.Equals(userName) && u.Password.Equals(password));
            if (user == null)
            {
                return null;
            }

            user.Status = true;
            db.SaveChanges();

            return user;
        }

        public void LogUserOut(int id)
        {
            User user = db.Users.Find(id);
            if(user != null)
            {
                user.Status = false;
                db.SaveChanges();

                ChatRoomDAO chatRoomDAO = new ChatRoomDAO();
                RoomMemberDAO roomMemberDAO = new RoomMemberDAO();
                RoomRequestDAO roomRequestDAO = new RoomRequestDAO();

                roomRequestDAO.DeleteAllRequestsOfOneUser(id);
                roomMemberDAO.DeleteOneMemberOfAllRooms(id);
                chatRoomDAO.DeleteChatRoomByUserId(id);
            }
        }

        public User GetUser(int id)
        {
            User user = db.Users.Find(id);

            return user;
        }

        public User GetUser(string userName)
        {
            User user = db.Users.SingleOrDefault(u => u.UserName.Equals(userName));

            return user;
        }

        public bool UpdateUser(User user)
        {
            User myUser = db.Users.SingleOrDefault(u => u.Id == user.Id);
            if(myUser != null)
            {
                myUser.Avatar = user.Avatar;
                myUser.FirstName = user.FirstName;
                myUser.LastName = user.LastName;
                myUser.Password = user.Password;
                myUser.Status = user.Status;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return false;
                }

                return true;
            }

            return true;
        }

        public bool AddUser(User user)
        {
            if (db.Users.SingleOrDefault(u => u.UserName.Equals(user.UserName)) != null)
            {
                return false;
            }
            //user.ProviderUserId = "A";
            db.Users.Add(user);
            db.SaveChanges();

            return true;
        }

        public User LogUserInByProvider(string provider, string providerUserId)
        {
            User us = new User();
            us.Id = -1;
            foreach (User u in db.Users)
            {

                if (!string.IsNullOrEmpty(u.ProviderUserId))
                {
                    if (u.ProviderUserId.CompareTo(providerUserId) == 0 && u.Provider.CompareTo(provider) == 0)
                    {
                        us = u;
                        us.Status = true;
                        break;

                    }
                }

            }

            if (us.Id == -1)
                return null;

            db.SaveChanges();
            return us;
        }
    }
}
